<?php
namespace laiq\db;

use PDO;
use PDOException;

class Connection 
{
    private static $_pdo = null;

    private function __construct() {
        try {
            $dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8', DB_HOST, DB_NAME);
            // PDO::ATTR_EMULATE_PREPARES=>FALSE防止预处理时将int转换成string
            $option = [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES=>FALSE];

            self::$_pdo = new PDO($dsn, DB_USER, DB_PASS, $option);
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
    }

    private function __clone() {
    }

    public static function getInstance() {
        if (self::$_pdo === null) {
            new self();
        }
        return self::$_pdo;
    }
 
}